package org.nearby.note.dialog;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.nearby.note.utils.Constant;
import org.nearby.note.utils.Message;
import org.nearby.note.utils.common;
import org.nearby.note.utils.props;

public class ServerStatusDlg extends JDialog {
    private JPanel mainPanel;

    private JLabel cpustatus;
    private JLabel memstatus;
    private JLabel tip;

    private int winWidth;
    private int winHeight;
    private HashMap<String, Integer> d = null;

    public ServerStatusDlg(JFrame parent, String title, boolean modal) {
        super(parent, title, modal);

        initWin();
        initBind();
    }

    private void initWin() {
        this.setLayout(null);
        this.winWidth = 500;
        this.winHeight = 500;
        this.setSize(winWidth, winHeight);

        d = common.getWindowCenterSize(winWidth, winHeight);
        this.setLocation(d.get("width"), d.get("height"));
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        tip = new JLabel("服务器状态");
        tip.setBounds(100, 20, 150, 20);

        cpustatus = new JLabel("cpu状态加载中...");
        cpustatus.setBounds(80, 80, 300, 20);

        memstatus = new JLabel("memory状态加载中...");
        memstatus.setBounds(80, 120, 300, 20);

        this.add(tip);
        this.add(cpustatus);
        this.add(memstatus);

        this.setResizable(false);
        this.setVisible(true);

        // 获取服务器状态
        new Thread(new MyThread()).start();
    }

    private void initBind() {
        /*
         * for(int i = 0; i<100000000; i++){ this.tip.setText(String.valueOf(i)); }
         */
        // JOptionPane.showMessageDialog(this, "测试测试");
    }

    public static void main(String[] args) {
        new ServerStatusDlg(null, "启动", false);
    }

    class MyThread implements Runnable {
        Socket clientSocket = null;
        Message sendMsg = new Message();
        Message recvMsg = null;
        
        BufferedReader br = null;
        PrintStream ps = null;
        String senddata = "";
        char[] recvdata = new char[Constant.MAX_PACKET];
        String tmpStr = "";
        
        

        @Override
        public void run() {
            try {
                clientSocket = new Socket(props.get("server_ip"), Integer.valueOf(props
                        .get("server_port")));
            } catch (NumberFormatException e1) {
                e1.printStackTrace();
            } catch (UnknownHostException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            // 输出
            try {
                ps = new PrintStream(clientSocket.getOutputStream());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            // 输入
            try {
                br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            while (true) {
                try {
                    sendMsg.setCommand(Constant.command.SOCKET_SERVERINFO);
                    sendMsg.setFromTo("");
                    sendMsg.setSendTo(Constant.SOCKET_SERVER);
                    sendMsg.setMsgBody("");
                    
                    senddata = sendMsg.Msg2String();
                    ps.print(senddata);

                    br.read(recvdata, 0, Constant.MAX_PACKET);
                    
                    recvMsg = Message.Str2Message(recvdata.toString());
                    tmpStr = recvMsg.getMsgBody();
                    
                    cpustatus.setText(tmpStr.split(Constant.SOCKET_MSG_SPLIT)[0]);
                    memstatus.setText(tmpStr.split(Constant.SOCKET_MSG_SPLIT)[1]);

                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("1: " + e.getMessage());
                    break;
                } catch (UnknownHostException e) {
                    System.out.println("2: " + e.getMessage());
                    break;
                } catch (IOException e) {
                    System.out.println("3: " + e.getMessage());
                    break;
                }

            }

            try {
                if (clientSocket != null)
                    clientSocket.close();
                if (ps != null)
                    ps.close();
                if (br != null)
                    br.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
            ServerStatusDlg.this.dispose();
        }
    }

}
